package com.hy.heath.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.heath.common.R;
import com.hy.heath.pojo.entity.VaccineRecord;
import com.hy.heath.service.VaccineRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/vaccine/records")
public class VaccineRecordController {

    @Autowired
    private VaccineRecordService recordService;

    // 根据用户ID查询
    @GetMapping("/user/{userId}")
    public R<List<VaccineRecord>> getByUser(@PathVariable Integer userId) {
        return R.success(recordService.listByUserId(userId));
    }

    // 分页查询
    @GetMapping("/page")
    public R<Page<VaccineRecord>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer userId) {
        return R.success(recordService.pageRecords(
                        new Page<>(pageNum, pageSize), userId));
    }

    // 查看详情
    @GetMapping("/{id}")
    public R<VaccineRecord> detail(@PathVariable Integer id) {
        return R.success(recordService.getById(id));
    }

    // 原有方法保持以下不变（新增校验逻辑已通过Service层实现）
    @PostMapping
    public R<String> add(@RequestBody VaccineRecord record) {
        recordService.save(record);
        return R.success("添加成功");
    }

    @PutMapping
    public R<String> update(@RequestBody VaccineRecord record) {
        recordService.updateById(record);
        return R.success("更新成功");
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        recordService.removeById(id);
        return R.success("删除成功");
    }
    // Controller添加接口
    @GetMapping("/expiring")
    // 根据传入的checkDate参数，获取即将过期的疫苗记录
    public R<List<VaccineRecord>> expiring(@RequestParam String checkDate) {
        // 将传入的checkDate参数转换为LocalDate类型
        return R.success(recordService.getExpiringRecords(
                LocalDate.parse(checkDate)));
    }
}